package sprinklerbee.views;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.RectangleInsets;

import sprinklerbee.factory.ServiceFactory;
import sprinklerbee.model.SchedulerHistory;

import java.awt.Color;
import java.util.List;

import javax.swing.JTree;
import java.awt.SystemColor;

public class ConsumptionDialog extends JDialog {

	private JPanel panel;
	private JPanel barChart;
	private JTree tree;
	private JPanel panel_3;
	private DefaultPieDataset dsPie = new DefaultPieDataset();

	/**
	 * Create the panel.
	 */
	public ConsumptionDialog() {
		this.setBounds(0, 0, 840, 500);
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 11, 800, 440);
		getContentPane().add(tabbedPane);

		panel = new JPanel();
		panel.setToolTipText("");
		tabbedPane.addTab("Monthly consumption", null, panel, null);
		panel.setLayout(null);

		barChart = new JPanel();
		barChart.setBorder(new TitledBorder(null, "",
				TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));
		barChart.setBounds(0, 0, 795, 533);
		panel.add(barChart);
		createMonthlyChart();

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(SystemColor.menu);
		tabbedPane.addTab("Consumption analysis", null, panel_1, null);
		panel_1.setLayout(null);

		JPanel panel_2 = new JPanel();
		panel_2.setBounds(0, 0, 150, 410);
		panel_1.add(panel_2);
		panel_2.setLayout(null);

		final JScrollPane typeTree = new JScrollPane();
		typeTree.setAutoscrolls(true);
		typeTree.setBounds(0, 0, 150, 410);
		panel_2.add(typeTree);

		panel_3 = new JPanel();
		panel_3.setBounds(160, 0, 620, 410);
		panel_1.add(panel_3);
		panel_3.setLayout(null);

		setPieDataset(ServiceFactory.getSchedulerServiceInstance().getAnalysis());
		createAnalysisChart();

		DefaultMutableTreeNode root = new DefaultMutableTreeNode(
				"All sprinklers");
		DefaultMutableTreeNode groups = new DefaultMutableTreeNode(
				"Specific group");
		root.add(groups);
		// add groups to the groups tree node
		groups.add(new DefaultMutableTreeNode("North"));
		groups.add(new DefaultMutableTreeNode("South"));
		groups.add(new DefaultMutableTreeNode("West"));
		groups.add(new DefaultMutableTreeNode("East"));

		tree = new JTree(root);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				String name = tree.getLastSelectedPathComponent().toString();
				if (name.equals("All sprinklers")) {
					setPieDataset(ServiceFactory.getSchedulerServiceInstance().getAnalysis());
				}
				if (name.equals("Specific group")) {
					setPieDataset(ServiceFactory
							.getSchedulerServiceInstance()
							.getAnalysisForGroup());
				}
				if (name.equals("North")) {
					setPieDataset(ServiceFactory
							.getSchedulerServiceInstance().getAnalysisByGroup(
									"North"));
				}
				if (name.equals("South")) {
					setPieDataset(ServiceFactory
							.getSchedulerServiceInstance().getAnalysisByGroup(
									"South"));
				}
				if (name.equals("West")) {
					setPieDataset(ServiceFactory
							.getSchedulerServiceInstance().getAnalysisByGroup(
									"West"));
				}
				if (name.equals("East")) {
					setPieDataset(ServiceFactory
							.getSchedulerServiceInstance().getAnalysisByGroup(
									"East"));
				}
				createAnalysisChart();
			}
		});
		tree.setBounds(0, 0, 100, 533);
		typeTree.setViewportView(tree);

	}

	private void createMonthlyChart() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		double total;
		int start = 1;
		int end = 3;
		for (; start <= end; start++) {
			total = ServiceFactory.getSchedulerServiceInstance()
					.getMonthlyConsumption(String.valueOf(start));
			String label = "";
			if (start == 1) {
				label = "Jan";
			}
			if (start == 2) {
				label = "Feb";
			}
			if (start == 3) {
				label = "Mar";
			}
			dataset.setValue(total, label, label);
		}
		JFreeChart chart = ChartFactory.createBarChart3D("Water consumption",
				"", "Gallon/Month", dataset, PlotOrientation.VERTICAL, true,
				false, false);
		try {
			ChartPanel pan = new ChartPanel(chart);
			pan.setBorder(new LineBorder(new Color(128, 128, 0), 1, false));
			pan.setSize(750, 450);
			pan.setVisible(true);
			barChart.add(pan);
		} catch (Exception e) {
		}

	}

	private void setPieDataset(List<SchedulerHistory> history) {
		if (null!=dsPie) {
			dsPie.clear();
		}
		String name;
		double value;
		for (SchedulerHistory schedulerHistory : history) {
			name = schedulerHistory.getSprinkler().getName();
			value = schedulerHistory.getConsumption();
			dsPie.setValue(name, value);
		}
	}

	private void createAnalysisChart() {
		PiePlot3D plot = new PiePlot3D(dsPie);
		plot.setDepthFactor(0.10);
		plot.setShadowPaint(null);
		plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
		plot.setDepthFactor(0.2);
		plot.setStartAngle(45);
		plot.setForegroundAlpha(0.8f);
		plot.setBackgroundAlpha(0.9f);
		JFreeChart chart = new JFreeChart("Water consumption analysis",
				JFreeChart.DEFAULT_TITLE_FONT, plot, true);
		ChartPanel pan = new ChartPanel(chart);
		pan.setBorder(new LineBorder(new Color(128, 128, 0), 1, false));
		pan.setBounds(0, 0, 600, 400);
		pan.setVisible(true);
		panel_3.add(pan);
	}

}
